package com.citcc.web.controller.business;

import com.citcc.business.bean.AppVersion;
import com.citcc.business.service.AppVersionService;
import com.citcc.business.service.PersonalCenterService;
import com.citcc.common.annotation.Log;
import com.citcc.common.base.AjaxResult;
import com.citcc.common.enums.BusinessType;
import com.citcc.common.page.TableDataInfo;
import com.citcc.common.utils.file.FileUploadUtils;
import com.citcc.framework.util.ShiroUtils;
import com.citcc.framework.web.base.BaseController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/appVersion")
public class AppVersionController extends BaseController {

    private String prefix = "business/appVersion";

    @Value(value="${citccIot.picUrl}")
    private String picUrl;

    @Autowired
    private AppVersionService appVersionService;
    @Autowired
    private PersonalCenterService personalCenterService;

    @RequiresPermissions("business:appVersion:view")
    @GetMapping("/main")
    public String driver(ModelMap map){
        map.put("count",appVersionService.queryExist());
        return prefix + "/main";
    }

    @RequiresPermissions("business:appVersion:list")
    @GetMapping("/list")
    @ResponseBody
    public TableDataInfo list(AppVersion appVersion)
    {
        startPage();
        if (appVersion.getOrgId()==null||"".equals(appVersion.getOrgId())) {
            appVersion.setOrgId(getSysUser().getDeptId());
        }
        List<AppVersion> list = appVersionService.queryAll(appVersion);
        return getDataTable(list);
    }

    @GetMapping("/add")
    public String toAdd()
    {
        return prefix + "/add";
    }

    /**
     * 新增升级信息
     */
    @RequiresPermissions("business:appVersion:add")
    @Log(title = "升级管理", businessType = BusinessType.INSERT)
    @PostMapping("/addSave")
    @ResponseBody
    public AjaxResult addSave(AppVersion appVersion)
    {
        appVersion.setCreateBy(ShiroUtils.getLoginName());
        appVersion.setDelFlag("0");
        return toAjax(appVersionService.insert(appVersion));
    }

    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id")String id, ModelMap map)
    {
        map.put("appVersion",appVersionService.getAppVersion(id));
        return prefix + "/edit";
    }

    /**
     * 修改司机信息
     */
    @RequiresPermissions("business:appVersion:edit")
    @Log(title = "版本管理", businessType = BusinessType.UPDATE)
    @PostMapping("/editAppVersion")
    @ResponseBody
    public AjaxResult editAppVersion(AppVersion appVersion)
    {
        //编辑版本先假删除原来的数据在新增新的数据
        appVersionService.delete(appVersion.getId());
        appVersion.setId(null);
        appVersion.setCreateBy(ShiroUtils.getLoginName());
        appVersion.setDelFlag("0");
        return toAjax(appVersionService.insert(appVersion));
    }

    @RequiresPermissions("business:appVersion:remove")
    @Log(title = "升级管理", businessType = BusinessType.UPDATE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String id)
    {
        try
        {
            Integer e = appVersionService.delete(id);
            return toAjax(e);
        }
        catch (Exception e)
        {
            return error(e.getMessage());
        }
    }

    /**
     * 上传文件
     */
    @PostMapping("/upload")
    @ResponseBody
    public AjaxResult upload(MultipartFile upFile)
    {
        try {
            String fileName = FileUploadUtils.uploadFile(upFile);
            String defaultBaseDir = FileUploadUtils.getDefaultBaseDir();
            String path = picUrl+"h5" + fileName;
            return success(path);
        } catch (IOException e) {
            e.printStackTrace();
            return error("文件上传失败");
        }
    }

    /**
     * 内部调用，昵称修改编码昵称
     *
     * @return
     */
    @RequestMapping(value = "/updateNickNameEncode")
    @ResponseBody
    public AjaxResult updateNickNameEncode() {
        personalCenterService.updateNickNameEncode();
        return AjaxResult.success();
    }

}
